package com.gitee.ywj1352.proxy.dynamic.cglib;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * Created by wenjie.yang on 3/25/2019.
 */
public class CglibProxy implements MethodInterceptor{

  public Object getInstance(Class<?> clazz) throws Exception{
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(this);
    return enhancer.create();
  }

  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
      throws Throwable {
    //业务的增强
    System.out.println("i am cglib proxy, i get you");
    methodProxy.invokeSuper(o,objects);
    System.out.println("i am cglib proxy, bye ");
    return o;
  }
}
